package com.wanban.download.db;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.wanban.download.model.M3U8;
import com.wanban.download.model.Status;
import java.util.HashMap;

public class DownloadEntity implements Parcelable {

    private final String url;
    public String videoName;
    public DownloadEntity(String url, String videoName){
        this.url = url;
        this.videoName = videoName;
    }

    public String getUrl(){
        return url;
    }

    @Status
    public int state;
    public int episode;
    public int sourceId;
    public int category;
    public int downloadType;
    public long videoId;
    public long watchTime;
    public long createTime;
    public long contentLength;
    public long tempContentLength;
    public float percent;

    // 文件路径
    public String playPath;
    // 图片URL
    public String coverUrl;
    // 出错消息
    public String errorMsg;
    public String episodeName;
    // 下载文件根路径，与是否使用sdcard有关
    public String fileRootPath;
    // 封面路径
    public String coverFilePath;
    public String extra;

    // header
    public HashMap<String, String> header;

    /**
     * 以下参数不参与数据库存储
     */
    // m3u8具体信息
    public M3U8 m3u8;
    public long speed;
    // 重试次数
    public int retryTime;
    public int episodeCount;
    public long startTaskTime;
    public boolean isSelected;
    // 是否因为网络导致出错
    public boolean isNetworkError;

    // 是否是短视频 0:false, 1:true
    public int skitsStatus;

    public String getDownloadTag() {
        return videoName + "_" + videoId + "_" + sourceId + "_" + episode;
    }

    public String getFirstLayerDirName(){
        return videoName + "_" + videoId;
    }

    public String getSecondLayerDirName(){
        return "sourceId_" + sourceId + "_episode_" + episode;
    }

    public String getCoverImagePath(){
        if (TextUtils.isEmpty(coverFilePath)) {
            return coverUrl;
        } else {
            return coverFilePath;
        }
    }
    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
        @Override
        public DownloadEntity createFromParcel(Parcel in) {
            return new DownloadEntity(in);
        }

        @Override
        public DownloadEntity[] newArray(int size) {
            return new DownloadEntity[size];
        }
    };


    protected DownloadEntity(Parcel in) {
        url = in.readString();
        videoName = in.readString();
        state = in.readInt();
        episode = in.readInt();
        sourceId = in.readInt();
        category = in.readInt();
        downloadType = in.readInt();
        videoId = in.readLong();
        watchTime = in.readLong();
        createTime = in.readLong();
        contentLength = in.readLong();
        tempContentLength = in.readLong();
        percent = in.readFloat();
        playPath = in.readString();
        coverUrl = in.readString();
        errorMsg = in.readString();
        episodeName = in.readString();
        fileRootPath = in.readString();
        coverFilePath = in.readString();
        speed = in.readLong();
        retryTime = in.readInt();
        episodeCount = in.readInt();
        startTaskTime = in.readLong();
        isSelected = in.readByte() != 0;
        isNetworkError = in.readByte() != 0;
        skitsStatus = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(url);
        dest.writeString(videoName);
        dest.writeInt(state);
        dest.writeInt(episode);
        dest.writeInt(sourceId);
        dest.writeInt(category);
        dest.writeInt(downloadType);
        dest.writeLong(videoId);
        dest.writeLong(watchTime);
        dest.writeLong(createTime);
        dest.writeLong(contentLength);
        dest.writeLong(tempContentLength);
        dest.writeFloat(percent);
        dest.writeString(playPath);
        dest.writeString(coverUrl);
        dest.writeString(errorMsg);
        dest.writeString(episodeName);
        dest.writeString(fileRootPath);
        dest.writeString(coverFilePath);
        dest.writeLong(speed);
        dest.writeInt(retryTime);
        dest.writeInt(episodeCount);
        dest.writeLong(startTaskTime);
        dest.writeByte((byte) (isSelected ? 1 : 0));
        dest.writeByte((byte) (isNetworkError ? 1 : 0));
        dest.writeInt(skitsStatus);
    }
}
